<template>
  <div>
    <el-select v-model="value" placeholder="请选择" @change="stationSelectChange">
      <el-option
        v-for="item in stationList"
        :key="item.id"
        :label="item.name"
        :value="item.id"
      />
    </el-select>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { mapMutations } from 'vuex'
export default {
  data() {
    return {
      value: ''
    }
  },
  computed: {
    ...mapGetters([
      'stationList'
    ])
  },
  watch: {
    stationList() {
      this.value = this.stationList[0].id
    }
  },
  mounted() {
    this.value = this.stationList ? this.stationList[0] : ''
    // console.log(this.value)
  },
  methods: {
    ...mapMutations(['user/SET_STATIONId', 'user/SET_WIRING', 'user/SET_NAME', 'user/SET_MODEL']),
    stationSelectChange(val) {
      // 查找指定id对应的数组
      const obj = this.stationList.find((item) => {
        return item.id === val
      })
      this['user/SET_WIRING'](obj.wiring)
      this['user/SET_MODEL'](obj.model)
      this['user/SET_NAME'](obj.name)
      this['user/SET_STATIONId'](val)

      // 方式二
      // this.$store.commit('user/SET_STATIONId', val)
    }
  }
}
</script>

